﻿/*
	Copyright (c) 2013 Andrew Meyer

	Permission is hereby granted, free of charge, to any person obtaining a copy of this software
	and associated documentation files (the "Software"), to deal in the Software without
	restriction, including without limitation the right to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
	Software is furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all copies or
	substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
	BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
	DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System;

namespace Ascend
{
	/// <summary>
	/// Class for specifying an Ascend.AnimatedSceneNode's animation within an Ascend.Timeline. This
	/// class cannot be inherited.
	/// </summary>
	public sealed class TimelineAnimation
	{
		#region Lifecycle
		/// <summary>
		/// Initializes a new instance of the Ascend.TimelineAnimation class from given
		/// Ascend.AnimatedSceneNode, animation name, start frame, and relative play rate.
		/// </summary>
		/// <param name="sceneNode">
		/// The Ascend.AnimatedSceneNode to be animated.
		/// </param>
		/// <param name="animationName">
		/// The animation to play.
		/// </param>
		/// <param name="start">
		/// The timeline frame at which to begin the animation.
		/// </param>
		/// <param name="relativePlayRate">
		/// The ratio of frames to progress for every one timeline frame. Specifying a negative
		/// value will reverse the animation.
		/// </param>
		/// <exception cref="System.ArgumentOutOfRangeException">
		/// Thrown if <paramref name="start"/> is less than zero.
		/// </exception>
		/// <exception cref="System.ArgumentException">
		/// Thrown if <paramref name="animationName"/> is not defined on
		/// <paramref name="sceneNode"/> -or- if <paramref name="relativePlayRate"/> is zero.
		/// </exception>
		public TimelineAnimation(AnimatedSceneNode sceneNode, string animationName, int start = 0,
								 double relativePlayRate = 1.0)
		{
			Initialize(sceneNode, animationName, relativePlayRate);
			SetFrameRange(start);
		}

		/// <summary>
		/// Initializes a new instance of the Ascend.TimelineAnimation class from given
		/// Ascend.AnimatedSceneNode, animation name, start frame, end frame, and relative play
		/// rate.
		/// </summary>
		/// <param name="sceneNode">
		/// The Ascend.AnimatedSceneNode to be animated.
		/// </param>
		/// <param name="animationName">
		/// The animation to play.
		/// </param>
		/// <param name="start">
		/// The timeline frame at which to begin the animation.
		/// </param>
		/// <param name="end">
		/// The timeline frame at which to end the animation.
		/// </param>
		/// <param name="relativePlayRate">
		/// The ratio of frames to progress for every one timeline frame. Specifying a negative
		/// value will reverse the animation.
		/// </param>
		/// <exception cref="System.ArgumentOutOfRangeException">
		/// Thrown if <paramref name="start"/> is less than zero or greater than
		/// <paramref name="end"/>.
		/// </exception>
		/// <exception cref="System.ArgumentException">
		/// Thrown if <paramref name="animationName"/> is not defined on
		/// <paramref name="sceneNode"/> -or- if <paramref name="relativePlayRate"/> is zero.
		/// </exception>
		public TimelineAnimation(AnimatedSceneNode sceneNode, string animationName, int start,
								 int end, double relativePlayRate = 1.0)
		{
			Initialize(sceneNode, animationName, relativePlayRate);
			SetFrameRange(start, end);
		}
		#endregion

		#region Public Properties
		/// <summary>
		/// Gets the Ascend.AnimatedSceneNode to be animated.
		/// </summary>
		public AnimatedSceneNode SceneNode
		{
			get;
			private set;
		}

		/// <summary>
		/// Gets the name of the animation to play.
		/// </summary>
		public string AnimationName
		{
			get;
			private set;
		}

		/// <summary>
		/// Gets the start Ascend.Timeline frame for SceneNode's animation, since the start of the
		/// Ascend.Timeline.
		/// </summary>
		public int Start
		{
			get;
			private set;
		}

		/// <summary>
		/// Gets the end Ascend.Timeline frame for the SceneNode's animation, since the start of the
		/// Ascend.Timeline.
		/// </summary>
		public int End
		{
			get;
			private set;
		}

		/// <summary>
		/// Gets the value specifying the ratio of frames to progress for every one Ascend.Timeline
		/// frame.
		/// </summary>
		public double RelativePlayRate
		{
			get;
			private set;
		}
		#endregion

		#region Private Methods
		private void Initialize(AnimatedSceneNode sceneNode, string animationName,
								double relativePlayRate)
		{
			if (!sceneNode.ListAnimations().Contains(animationName))
			{
				throw new ArgumentException(string.Format(
					"Animation '{0}' does not exist on AnimatedSceneNode.", animationName),
					"animationName");
			}

			if (relativePlayRate == 0.0)
			{
				throw new ArgumentException("Relative play rate must be a non-zero value.",
					"relativePlayRate");
			}

			SceneNode = sceneNode;
			AnimationName = animationName;
			RelativePlayRate = relativePlayRate;
		}

		private void SetFrameRange(int start, int end = -1)
		{
			if (start < 0 || (end != -1 && start > end))
			{
				throw new ArgumentOutOfRangeException("start", start,
					"Start frame must be non-negative and less than or equal to end frame.");
			}

			FrameInterval interval = SceneNode.GetAnimation(AnimationName);

			// Specify animation range for timeline.
			// If end is -1, use native animation length (plays through once)
			Start = start;
			End = (end == -1)
				? start + ToTimelineLength(interval.Length) - 1
				: end;
		}

		private int ToTimelineLength(int localAnimationLength)
		{
			return (int)Math.Ceiling(localAnimationLength / Math.Abs(RelativePlayRate));
		}
		#endregion
	}
}
